package com.jy.wms.common.base;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author:zzp
 * @Description: Created by Administrator on 2017/8/13.
 * @Date:Create in 21:40 2017/8/13
 */
public abstract class BaseService<M extends BaseDao<T>,T extends BaseModel> extends ServiceImpl<M,T> {
    /**
     * 日志对象
     */
    protected Logger logger = (Logger) LoggerFactory.getLogger(getClass());
    @Autowired
    protected M dao;
    private List<baseMenu> list;
    //获取菜单
    public JSONArray getMenu(Integer[] rid) {
        list = dao.getRoleMenus(rid);
        JSONArray array = new JSONArray();
        JSONArray array2 = new JSONArray();
        for(baseMenu menu : list){
            if("0".equals(menu.getFid())){
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu);
                jsonObject.put("children",build(menu.getId(),rid));
                array.add(jsonObject);
            }
        }
        return array;
    }


    private JSONArray build(String id, Integer[] rid){
        //leave+=1;
        JSONArray buildarray = new JSONArray();
        for (baseMenu menu2 : list) {
            if(menu2.getFid().equals(id)){
                /*menu2.setLeava(leave);*/
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu2);
                jsonObject.put("children",build(menu2.getId(),rid));
                buildarray.add(jsonObject);
                //lists.add(menu2);
            }
        }
        return buildarray;

    }

    /**
     * 获取所有菜单（不需要权限）
     * @param
     * @return
     */
    public JSONArray getMenus() {
        list= dao.getMenus();
        JSONArray array = new JSONArray();
        JSONArray array2 = new JSONArray();
        for(baseMenu menu : list){
            if("0".equals(menu.getFid())){
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu);
               // JSONArray jsonArray

                jsonObject.put("children",builds(menu.getId()));
                array.add(jsonObject);
            }
        }
        return array;
    }
    private JSONArray builds(String id){
        //leave+=1;
        JSONArray buildarray = new JSONArray();
        for (baseMenu menu2 : list) {
            if(menu2.getFid().equals(id)){
                /*menu2.setLeava(leave);*/
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu2);
                JSONArray arr=builds(menu2.getId());//获取制定父节点的儿子
                List<baseFunction>flist=dao.functionList(menu2.getId());//获取菜单下的所有功能
                for(baseFunction bf:flist){
                    arr.add(bf);
                }
                jsonObject.put("children",arr);

                buildarray.add(jsonObject);
                //lists.add(menu2);
            }
        }
        return buildarray;
    }

}
